<script src="https://cdn.jsdelivr.net/npm/cdnbye@0.1.1"></script>
<video id="video" controls></video>
<p id="version"></p>
<h3>download info:</h3>
<p id="info"></p>
<table id="table-body">
    <tbody ></tbody>
</table>
<script>
    document.querySelector('#version').innerText = `hls.js version: ${Hls.version}  cdnbye version: ${Hls.pluginVersion}`;
    if(Hls.isSupported()) {
        var video = document.getElementById('video');
        var hls = new Hls();
        hls.loadSource('https://video-dev.github.io/streams/x36xhzz/url_2/193039199_mp4_h264_aac_ld_7.m3u8');
        hls.attachMedia(video);
        hls.on(Hls.Events.MANIFEST_PARSED,function(event, data) {
            video.play();
        });
        var total = 0, p2p = 0;
        hls.on(Hls.Events.FRAG_LOADED, (id, data) => {
            var frag = data.frag;
            console.log(`frag.loadByP2P ${frag.loadByP2P} frag.loadByHTTP ${frag.loadByHTTP}`)
            var source = undefined;
            if (frag.loadByHTTP === true) {
                source = 'HTTP';
            } else if (frag.loadByP2P === true) {
                source = 'P2P';
            }
            addToTable(frag.relurl, frag.loaded, source);
            total += frag.loaded;
            if (source === 'P2P') p2p += frag.loaded;
            document.querySelector('#info').innerText = `p2p ratio: ${Math.round(p2p/total*100)}%   saved traffic: ${Math.round(p2p/1024)}KB`;
        });
        function addToTable(url, downloaded, source) {
            var infoStr = `download ${url}(size:${downloaded}B) by ${source}`;
            document.querySelector('#table-body tbody').innerHTML +=
                `<tr style="text-align: center">
                    <td>${infoStr}</td>
                </tr>`
        };
    }
</script>